#!/usr/bin/perl -w
use Getopt::Long;
use FindBin;
use strict;

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888


my $usage = <<'USAGE';

USAGE:

    panda_omim_codes.pl --morbid_omim FILE --genemap_omim FILE
	                    --db_omim FILE [OPTIONS]...  DB_NAME

	MANDATORY arguments:
			
		--morbid_omim FILE   the OMIM morbid map available from
		                           ftp://ftp.ncbi.nih.gov/repository/OMIM/morbidmap
		--genemap_omim FILE  the OMIM gene map available from
		                           ftp://ftp.ncbi.nih.gov/repository/OMIM/genemap
		--db_omim FILE       the full OMIM database available as a compressed archive from
		                           ftp://ftp.ncbi.nih.gov/repository/OMIM/omim.txt.Z

	Optional arguments:

		[--user USER_NAME]   the postgreSQL user name of the owner/panda administrator
        [--host STRING]      the postgreSQL database server
        [--password STRING]  the postgreSQL database password


        [--help]


RESULTS:


    This script creates the panda system with DB_NAME as the PostgreSQL database
                behind panda.
	
USAGE

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888


my $user			= 'postgres';
my $password		= '';
my $host			= '';
my $clear_first;
my $recreate_db;
my $genemap_omim	= '';
my $morbid_omim 	= '';
my $db_omim     	= '';
my $help			= '';
my $dbname			= '';

GetOptions (
			
			'help'				=> \$help,
			'user=s'			=> \$user,
			'password'			=> \$password,
			'host=s'			=> \$host,
			'clear_first' 		=> \$clear_first,
			'recreate_db'		=> \$recreate_db,
			);


die $usage if ($help);
$dbname			= $ARGV[0] if ($ARGV[0]);
die "\nError:\n\tMissing database name.\n$usage" unless ($dbname);
die "\nError:\n\tMissing genemap_omim name.\n$usage" unless ($genemap_omim);
die "\nError:\n\tMissing morbid_omim  name.\n$usage" unless ($morbid_omim );
die "\nError:\n\tMissing db_omim      name.\n$usage" unless ($db_omim     );
die "\nError:\n\tMissing user name.\n$usage" unless ($user);
$host		= '--host=' 	. $host if ($host);
$password	= '--password '			if ($password);



#_________________________________________________________________________________________

#	Parse OMIM codes from the gene map

#_________________________________________________________________________________________
my %MIM_codes_gm;
print STDERR "Parsing OMIM codes from the OMIM gene map...\n";
{
	open GENEMAP_OMIM, $genemap_omim or die "Error\n\tCould not open the [$genemap_omim] file\n$@\n";
	my $cnt_MIMs;
	while (<GENEMAP_OMIM>)
	{
		/	^							# start at beginning of line
			(?:[^|]*\|){9}				# ignore first 9 fields
			([^|]*)\|					# save MIM code field (not including | terminator)
			(?:[^|]*\|){3}				# ignore next 3 fields
			([^|]*)						# save disease field
							/x;
		if ($1 ne '' && $2 ne '')
		{
			my ($MIM_code, $dis) = ($1,$2);
			
			if ($dis !~ /^(		[?\[]		|		# not begin with ? or [
								(\{\?)		)	/x)	# or {?
			{
				++$MIM_codes_gm{$MIM_code};
				++$cnt_MIMs;
			}
		}
	}
	print STDERR "\t", scalar keys %MIM_codes_gm, "\tMIM codes were retrieved from the morbid map.\n";
	print STDERR "\t\t($cnt_MIMs disease entries were associated with MIM codes.)\n";
}	





#_________________________________________________________________________________________

#	Parse OMIM codes from the morbid map

#_________________________________________________________________________________________
my %MIM_codes_mm;
print STDERR "Parsing OMIM codes from the morbid map...\n";
{
	open MORBID_OMIM , $morbid_omim  or
				die "Error\n\tCould not open the [$morbid_omim] file\n$@\n";
	my $cnt_MIMs;
	while (<MORBID_OMIM>)
	{
		next unless
		/	^							# start at beginning of line
			(?:[^|]*\|){2}				# ignore first 2 fields
			(\d+)\|					    # save MIM code field (not including | terminator)
							/x;
		++$MIM_codes_mm{$1};
		++$cnt_MIMs;
	}
	print STDERR "\t", scalar keys %MIM_codes_mm, "\tMIM codes were retrieved from the morbid map.\n";
	print STDERR "\t\t($cnt_MIMs disease entries were associated with MIM codes.)\n";
}	



#_________________________________________________________________________________________

#	Parse OMIM codes associated with variants from the OMIM gene map

#_________________________________________________________________________________________
my %MIM_codes_mm;
print STDERR "Parse OMIM codes associated with variants from the OMIM gene map...\n";
{
	open DB_OMIM, "| lookup_variants_in_omim " . $db_omim
			or die "Error\n\t"
					"Could not run [lookup_variants_in_omim] to parse OMIM codes "
					"associated with variants in the OMIM db [$db_omim]\n"
					"lookup_variants_in_imim can be built by running (in this directory):\n"
					"gmake -f /bin/lookup_variants_in_omim/lookup_variants_in_omim.mak CFG=Release\n"
					"[$@]\n";
	my $cnt_MIMs;
	while (<MORBID_OMIM>)
	{
		next unless
		/	^							# start at beginning of line
			(?:[^|]*\|){2}				# ignore first 2 fields
			(\d+)\|					    # save MIM code field (not including | terminator)
							/x;
		++$MIM_codes_mm{$1};
		++$cnt_MIMs;
	}
	print STDERR "\t", scalar keys %MIM_codes_mm, "\tMIM codes were retrieved from the morbid map.\n";
	print STDERR "\t\t($cnt_MIMs disease entries were associated with MIM codes.)\n";
}	







sub uniquify(\@)
{
	my %hash;
	@hash{@{$_[0]}} = ();
	@{$_[0]} = keys %hash;	
	return @{$_[0]};
}	


print("Getting list of MIM codes\n\tassociated with OMIM missense variants in $records records:");
	`lookup_variants_in_omim 1000 <  $omim_root/omim.txt  >> omim.list`;

system ("/net/cpp-group/Leo/bin/uniquify.pl -n -v omim.list > omim_unique.list");
